package treballfinalUF2;

import java.sql.*;
import java.util.regex.Pattern;

import com.sun.org.apache.xerces.internal.impl.xs.identity.Selector.Matcher;



public class operacionsAmbBBDD extends Connexio implements java.io.Serializable {
	String Missatge = null;
	String nom = null;
	String password = null;
	
	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//Connexio ObjBdades = new Connexio();
	public operacionsAmbBBDD() {
		super();
		// TODO Auto-generated constructor stub
	
	}
	
	public void posaVisibleEmresa(String idEmpresa) throws SQLException{
		establishConnection();
		String sql = "UPDATE `miBaseUF2FinalJSP`.`empresa` SET `visible` = '1' WHERE `empresa`.`idempresa` ="+idEmpresa+";";
		PreparedStatement ps =  getMiConexion().prepareStatement(sql);
		ps.executeUpdate();
		closeMiConnexion();
	}
	
	
	public ResultSet retornaEmpreses() throws SQLException{
		establishConnection();		
		String sql = "SELECT * FROM miBaseUF2FinalJSP.empresa ";
		PreparedStatement ps =  getMiConexion().prepareStatement(sql);
		ps.executeQuery(sql);
		ResultSet result = ps.executeQuery(sql);	
		
		return result;
	}
	
	
	
	
	public boolean insertEmpresa(String nomEmpresa,String passEmpresa, String mailEmpresa,String descEmpresa) throws SQLException{
		establishConnection();
		boolean estat= false;
		try{
		String sql = "INSERT INTO miBaseUF2FinalJSP.empresa"
				+ "(nomempresa, password,mail,descripcio,visible)" + " VALUES('" + nomEmpresa
				+ "','" + passEmpresa+ "','"+mailEmpresa +  "','" + passEmpresa+ "','"+ 0 +"')";
		PreparedStatement ps =  getMiConexion().prepareStatement(sql);
		
				ps.executeUpdate(sql);
				estat = true;
		System.out.print("Creacio empresa ok" + estat);
		}catch(Exception e){
			
			System.out.print("Creacio empresa fail" + e);
		}

		
		closeMiConnexion();
		return estat;	
	}
	
	
	public boolean insertAlumne(String nomAlumne,String passAlumne, String mailAlumne) throws SQLException{
		establishConnection();
		boolean estat= false;
		try{
		String sql = "INSERT INTO miBaseUF2FinalJSP.alumne"
				+ "(nomalumne, password,mail,visible)" + " VALUES('" + nomAlumne
				+ "','" + passAlumne+ "','"+mailAlumne + "','"+ 0 +"')";
		PreparedStatement ps =  getMiConexion().prepareStatement(sql);
		
				ps.executeUpdate(sql);
				estat = true;
		System.out.print("Creacio alumne ok  " + estat);
		}catch(Exception e){
			
			System.out.print("Creacio alumne fail  " + e);
		}

		
		closeMiConnexion();
		return estat;	
	}
	
	
	
	
	
	
	
	
	
	
	//aquí es fa la comprovació del correu
	public boolean checkmail(String correo) {
	    Pattern pat = null;
	    java.util.regex.Matcher mat = null;
	    pat = Pattern.compile("^([0-9a-zA-Z]([_.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+([a-zA-Z]{2,9}.)+[a-zA-Z]{2,3})$");
	    mat = pat.matcher(correo);
	    if (mat.find()) {
	        System.out.println("[" + mat.group() + "]");
	        return true;
	    }else{
	        return false;
	    }
	}
	
	
	
	public String getMissatge() {
		return Missatge;
	}
	public void setMissatge(String missatge) {
		Missatge = missatge;
	}

}
